package cn.gwm.flink.streaming.dwm.charge.assist;

import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.dwm.charge.model.ChargeConstant;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.lang.reflect.Field;

/**
 * @ClassName ChargeFieldDram
 * @Description 非充电相关字段过滤   可替代 ChargeFieldDramFunction
 * @Author LiangGuang
 * @Date 2023/04/19 14:32
 */

public class ChargeFieldDram {


    private static final Field[] fields = ChargeConstant.class.getFields();

    public static JSONObject fieldDram(JSONObject source, ChargeConstant constant) {
        JSONObject json = JSONUtil.createObj();
        for (Field field : fields) {
            Object value = BeanUtil.getFieldValue(constant, field.getName());
            if (source.containsKey(value)) {
                json.set((String) value, source.get(value));
            } else {
                json.set((String) value, BaseFields.defaultVal);
            }
        }
        json.set(BaseFields.tid, source.getLong(BaseFields.tid));
        json.set(BaseFields.vin, source.getStr(BaseFields.vin));

        // 去掉无关
        json.remove(constant.summar);
        json.remove(constant.bandSummar);
        json.remove(constant.detail);
        json.remove(constant.favourable);
        json.remove(constant.powerType);
        return json;
    }
}